package com.avengers.core.model;

import com.avengers.core.model.db.SqlKey;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yu.wang
 * @since 2018-05-10 下午2:12
 */
public class SqlCondition implements SqlKey {
    public SqlCondition() {
        conditionSql = new StringBuilder();
        paramList = new ArrayList<>();
        forUpdate = false;
        limit = EMPTY;
        order = EMPTY;
        group = EMPTY;
        this.showLog = true;
    }

    protected String toSql() {
        StringBuilder conditionSql = new StringBuilder(this.conditionSql);

        String sql = conditionSql.append(SPACE).append(group).append(SPACE).append(order).append(SPACE).append(limit).append(forUpdate ? FOR_UPDATE : EMPTY).toString().replaceAll("[ ]{2,}", SPACE).trim();
        if (sql.startsWith(AND.trim())) {
            return sql.substring(3).trim();
        } else if (sql.startsWith(OR.trim()) && !sql.startsWith(ORDER_BY)) {
            return sql.substring(2).trim();
        }
        return sql.trim();
    }

    private StringBuilder conditionSql;
    private StringBuilder updateSetSql = null;
    private String limit;
    private String order;
    private String group;
    private List<Object> paramList;
    private List<Object> updateParamList;
    private boolean forUpdate;
    private boolean showLog;


    public StringBuilder getConditionSql() {
        return conditionSql;
    }

    public void setConditionSql(StringBuilder conditionSql) {
        this.conditionSql = conditionSql;
    }

    public StringBuilder getUpdateSetSql() {
        return updateSetSql;
    }

    public void setUpdateSetSql(StringBuilder updateSetSql) {
        this.updateSetSql = updateSetSql;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public List<Object> getParamList() {
        return paramList;
    }

    public void setParamList(List<Object> paramList) {
        this.paramList = paramList;
    }

    public List<Object> getUpdateParamList() {
        return updateParamList;
    }

    public void setUpdateParamList(List<Object> updateParamList) {
        this.updateParamList = updateParamList;
    }

    public boolean isForUpdate() {
        return forUpdate;
    }

    public void setForUpdate(boolean forUpdate) {
        this.forUpdate = forUpdate;
    }

    public void clearCondition() {
        this.conditionSql = new StringBuilder();
        this.paramList = new ArrayList<>();
        forUpdate = false;
        limit = EMPTY;
        order = EMPTY;
        group = EMPTY;

        if (null != this.updateSetSql) {
            this.updateSetSql = null;
            this.updateParamList = null;
        }
    }

    public boolean isShowLog() {
        return showLog;
    }

    public void setShowLog(boolean showLog) {
        this.showLog = showLog;
    }
}
